package gofish.servlets;

import gofish.json.JsonObject;
import gofish.objects.Game;
import gofish.objects.GameData;

/**
 * GetGameState will be a Servlet that Adam's client code can
 * contact. Adam's code will contact this Servlet when the client
 * realizes (due to a refresh() call) that he/she needs to update
 * their game state.
 *
 * @author Matt Lauber
 */
public class GetGameState {
	
	/**
	 * doPost() is called by the client when they need to get the
	 * latest game state information. This will return to the client
	 * as a JSON the game state in this format:
	 *
	 *  {
	 *    "player" :    {
	 *                    "hand" : ["5H", "6D", etc],
	 *                    "pairs" : ["4", "Q", etc],
	 *                  },
	 *    "opponents" : [
	 *                    {
	 *                      "name" : string playerName,
	 *                      "hand" : int numberOfCards,
	 *                      "pairs" : ["2", "K", etc],
	 *                    },
	 *                    {
	 *                      "name" : string playerName,
	 *                      "hand" : int numberOfCards,
	 *                      "pairs" : ["2", "K", etc],
	 *                    },
	 *                    etc
	 *                  ],
	 *    "turn" : string playerName,
	 *    "winners" : [ string playerName, string playerName, etc ]
	 *  }
	 *
	 * NOTE: the "winners" array is only added on if the game is over.
	 *
	 * Notice that the JsonObject class is overloaded to take care of
	 * all the JSON encoding.
	 */
	public Game doPost(int gid, String userName) {
		Game game = GameData.getGameState(gid);
		JsonObject response = new JsonObject(game, userName);
		System.out.println(response);
		
		return game;
	}

}
